﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using LunwenYun.Infrastructure.Entities.Repository;

namespace LunwenYun.Infrastructure.Entities.Entity
{
    public class PayLog : IAccessible<T_PayLog, PayLog>, ILogicEntity<T_PayLog, PayLog>
    {
        #region Fileds
        public int Id { get; set; }

        public string PayNo { get; set; }

        public DateTime PayDate { get; set; }

        public int TradeId { get; set; }

        public decimal Price { get; set; }

        /// <summary>
        /// 支付结果描述
        /// </summary>
        public string PayMessage { get; set; }
        /// <summary>
        /// 支付返回原文
        /// </summary>
        public string PayBackString { get; set; }
        /// <summary>
        /// 支付返回时间
        /// </summary>
        public DateTime PayBackDate { get; set; }
        /// <summary>
        /// 答复内容
        /// </summary>
        public string AnswerString { get; set; }
        /// <summary>
        /// 答复时间
        /// </summary>
        public DateTime AnswerDate { get; set; }

        /// <summary>
        /// 支付状态
        /// 1：新支付
        /// 2：支付成功
        /// </summary>
        public int PayStatus { get; set; }
        #endregion

        protected override void Covariant(T_PayLog model)
        {
            if (model != null)
            {
                this.AnswerDate = model.AnswerDate;
                this.AnswerString = model.AnswerString;
                this.Id = model.Id;
                this.PayBackDate = model.PayBackDate;
                this.PayBackString = model.PayBackString;
                this.PayDate = model.PayDate;
                this.PayMessage = model.PayMessage;
                this.PayNo = model.PayNo;
                this.PayStatus = model.PayStatus;
                this.Price = model.Price;
                this.TradeId = model.TradeId;
            }
        }

        protected override T_PayLog Inverter(PayLog model)
        {
            if (model != null)
            {
                return new T_PayLog()
                {
                    AnswerDate = model.AnswerDate,
                    AnswerString = model.AnswerString,
                    Id = model.Id,
                    PayBackDate = model.PayBackDate,
                    PayBackString = model.PayBackString,
                    PayDate = model.PayDate,
                    PayMessage = model.PayMessage,
                    PayNo = model.PayNo,
                    PayStatus = model.PayStatus,
                    Price = model.Price,
                    TradeId = model.TradeId
                };
            }
            return null;
        }

        public PayLog Set(T_PayLog dbModel)
        {
            Covariant(dbModel);
            return this;
        }

        public static string PayNoGenerator(int tradeId)
        {
            return String.Format("{0}000{1}{2}{3}", "1",
                DateTime.Now.ToString("yyyyMMddHHmmss"), new Random().Next(1, 1000).ToString().PadLeft(4, '0'),
                tradeId.ToString());
        }
    }
}
